import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

/**
 * AddEventWindow.java
 * 
 * Provides a window to add events to the database
 * 
 * @author Steven Glazer
 * @author Kyle Marcotte
 * @author Sam Milton
 */
public class AddEventWindow extends JDialog implements ActionListener {
	
	private static final long serialVersionUID = -1987272009263189086L;
	
	// Class Variables
	private final int HGAP = 12;
	private final int VGAP = 12;
	private final Integer[] MONTHS = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
	private final Integer[] DAYS = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
			21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
	private final Integer[] YEARS = {2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
			2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2022, 2023, 2024, 2025, 2026, 
			2027, 2028, 2029, 2030};
	
	private JList listData;
	private JScrollPane listDataScroll;
	private JPanel pane;
	private JPanel outerPane;
	private JPanel topPane;
	private JPanel infoPane;
	private JPanel timePaneA;
	private JPanel timePaneB;
	private JPanel buttonPane;
	private JButton selectBut;
	private JButton cancelBut;
	private JButton addBut;
	private JTextField nameInput;
	private JTextField locationInput;
	private JTextField costInput;
	private JComboBox sMonthInput;
	private JComboBox sDayInput;
	private JComboBox sYearInput;
	private JTextField sTimeInput;
	private JComboBox eMonthInput;
	private JComboBox eDayInput;
	private JComboBox eYearInput;
	private JTextField eTimeInput;
	private boolean selected = false;
	private AddEventWindow self = this;
	private Vector<String> timings = new Vector<String>();
	private Vector<EventDate> starts = new Vector<EventDate>();
	private Vector<EventDate> ends = new Vector<EventDate>();
	
	/**
	 * Constructor
	 */
	public AddEventWindow(JFrame parent, boolean modal) {
		super(parent, modal);
		this.setTitle("New Event");
		pane = (JPanel)this.getContentPane();
		pane.setLayout(new BorderLayout(HGAP, VGAP));
		pane.setBorder(BorderFactory.createEmptyBorder(VGAP, HGAP, VGAP, HGAP));
		
		buttonPane = new JPanel();
		buttonPane.setLayout(new GridLayout(1, 2, HGAP, VGAP));
		outerPane = new JPanel();
		outerPane.setLayout(new GridLayout(2, 1, HGAP, VGAP));
		topPane = new JPanel();
		topPane.setLayout(new GridLayout(3, 1, HGAP, VGAP));
		infoPane = new JPanel();
		infoPane.setLayout(new GridLayout(1, 3, HGAP, VGAP));
		timePaneA = new JPanel();
		timePaneA.setLayout(new GridLayout(1, 6, HGAP, VGAP));
		timePaneB = new JPanel();
		timePaneB.setLayout(new GridLayout(1, 6, HGAP, VGAP));
		
		listData = new JList();
		listDataScroll = new JScrollPane(listData);
		
		addBut = new JButton("+");
		selectBut = new JButton("Add");
		selectBut.addActionListener(this);
		cancelBut = new JButton("Cancel");
		cancelBut.addActionListener(this);
		
		nameInput = new JTextField("Name");
		locationInput = new JTextField("Location");
		costInput = new JTextField("Cost");
		sMonthInput = new JComboBox(MONTHS);
		sDayInput = new JComboBox(DAYS);
		sYearInput = new JComboBox(YEARS);
		eMonthInput = new JComboBox(MONTHS);
		eDayInput = new JComboBox(DAYS);
		eYearInput = new JComboBox(YEARS);
		
		Calendar cal = Calendar.getInstance();
		String min = "" + cal.get(Calendar.MINUTE);
		if (min.length() == 1)
			min = "0" + min;
		sMonthInput.setSelectedItem(cal.get(Calendar.MONTH));
		sDayInput.setSelectedItem(cal.get(Calendar.DAY_OF_MONTH));
		sYearInput.setSelectedItem(cal.get(Calendar.YEAR));
		String sTime = "" + cal.get(Calendar.HOUR_OF_DAY) + ":" + min;
		eMonthInput.setSelectedItem(cal.get(Calendar.MONTH));
		eDayInput.setSelectedItem(cal.get(Calendar.DAY_OF_MONTH));
		eYearInput.setSelectedItem(cal.get(Calendar.YEAR));
		String eTime = "" + cal.get(Calendar.HOUR_OF_DAY) + ":" + min;
		
		sTimeInput = new JTextField(sTime);
		eTimeInput = new JTextField(eTime);
		
		
		buttonPane.add(selectBut);
		buttonPane.add(cancelBut);
		
		timePaneA.add(new JLabel("Start:"));
		timePaneA.add(sMonthInput);
		timePaneA.add(sDayInput);
		timePaneA.add(sYearInput);
		timePaneA.add(sTimeInput);
		timePaneA.add(new JLabel(""));
		timePaneB.add(new JLabel("End:"));
		timePaneB.add(eMonthInput);
		timePaneB.add(eDayInput);
		timePaneB.add(eYearInput);
		timePaneB.add(eTimeInput);
		timePaneB.add(addBut);
		
		infoPane.add(nameInput);
		infoPane.add(locationInput);
		infoPane.add(costInput);
		
		topPane.add(infoPane);
		topPane.add(timePaneA);
		topPane.add(timePaneB);
		
		outerPane.add(topPane);
		outerPane.add(listDataScroll);
		
		pane.add(outerPane, BorderLayout.CENTER);
		pane.add(buttonPane, BorderLayout.SOUTH);
		
		addBut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					int sHour = Integer.parseInt(sTimeInput.getText().split(":")[0]);
					int sMin = Integer.parseInt(sTimeInput.getText().split(":")[1]);
					int eHour = Integer.parseInt(eTimeInput.getText().split(":")[0]);
					int eMin = Integer.parseInt(eTimeInput.getText().split(":")[1]);
				
					
					GregorianCalendar startCal = new GregorianCalendar(((Integer)sYearInput.getSelectedItem()).intValue(), 
							((Integer)sMonthInput.getSelectedItem()).intValue() - 1, 
							((Integer)sDayInput.getSelectedItem()).intValue(),
							sHour, sMin);
					
					GregorianCalendar endCal = new GregorianCalendar(((Integer)eYearInput.getSelectedItem()).intValue(), 
							((Integer)eMonthInput.getSelectedItem()).intValue() - 1, 
							((Integer)eDayInput.getSelectedItem()).intValue(),
							eHour, eMin);
					
					if (startCal.before(endCal) || startCal.equals(endCal)) {
						String start = "Start: " + sMonthInput.getSelectedItem() + "/" + sDayInput.getSelectedItem() + "/" +
								sYearInput.getSelectedItem() + "  " + sTimeInput.getText();
						String end = "End: " + eMonthInput.getSelectedItem() + "/" + eDayInput.getSelectedItem() + "/" +
								eYearInput.getSelectedItem() + "  " + eTimeInput.getText();
						timings.add(start + "\t\t\t" + end);
						listData.setListData(timings);
						starts.add(new EventDate(""+startCal.get(GregorianCalendar.YEAR), 
								""+sMonthInput.getSelectedItem(), 
								""+startCal.get(GregorianCalendar.DAY_OF_MONTH), 
								""+sHour, ""+sMin)); 
						ends.add(new EventDate(""+endCal.get(GregorianCalendar.YEAR), 
								""+eMonthInput.getSelectedItem(), 
								""+endCal.get(GregorianCalendar.DAY_OF_MONTH), 
								""+eHour, ""+eMin)); 
					}
					else
						JOptionPane.showMessageDialog(self, "Start Date must be before End Date", 
								"Event Time Warning", JOptionPane.WARNING_MESSAGE);
					
				} catch (NumberFormatException ex) {
					JOptionPane.showMessageDialog(self, "Times must be proper format\n Example: 18:30", 
							"Event Time Warning", JOptionPane.WARNING_MESSAGE);
				}
			}
		});
		
		this.pack();
		this.setLocationRelativeTo(parent);
		this.setVisible(true);
	}
	
	/**
	 * actionPerformed method of ActionListener
	 * 
	 * @param e		ActionEvent performed
	 */
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == selectBut && starts.size() > 0 && ends.size() > 0 && nameInput.getText().length() > 0) {
			try {
				Double.parseDouble(costInput.getText());
				selected = true;
				this.setVisible(false);
			} catch (NumberFormatException ex) {
				JOptionPane.showMessageDialog(this, "Cost must be an amount\n Example: 30.57", "Event Amount Warning", 
						JOptionPane.WARNING_MESSAGE);
			}
		}
		else if (e.getSource() == selectBut && nameInput.getText().length() > 0)
			JOptionPane.showMessageDialog(this, "Event needs at least one time", "Event Time Warning", 
					JOptionPane.WARNING_MESSAGE);
		else if (e.getSource() == selectBut)
			JOptionPane.showMessageDialog(this, "Event needs a name", "Event Name Warning", 
					JOptionPane.WARNING_MESSAGE);
		else
			this.setVisible(false);
	}
	
	/**
	 * Returns the information about the event
	 * 
	 * @return	List of Strings of the event information
	 */
	public String[] getResult() {
		if(selected) {
			
			String[] result = new String[3];
			result[0] = nameInput.getText();
			result[1] = locationInput.getText();
			result[2] = costInput.getText();
			return result;
		}
		else
			return null;
	}
	
	/**
	 * Returns the start times for the event
	 * 
	 * @return	Vector of the EventDates for start times
	 */
	public Vector<EventDate> getStartTimes() {
		if(selected)
			return starts;
		else
			return null;
	}
	
	/**
	 * Returns the end times for the event
	 * 
	 * @return	Vector of the EventDates for end times
	 */
	public Vector<EventDate> getEndTimes() {
		if (selected)
			return ends;
		else
			return null;
	}
}
